package com.ruihu.admin.module.business.electric.config.manager;

import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.module.business.electric.config.dao.ElectricConfigDao;
import com.ruihu.admin.module.business.electric.config.domain.dto.ElectricConfigDTO;
import com.ruihu.admin.module.business.electric.config.domain.entity.ElectricConfigEntity;
import com.ruihu.admin.util.BeanCopierUtil;
import com.ruihu.admin.util.RequestTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ElectricConfigManager {

    @Autowired
    private ElectricConfigDao electricConfigDao;



    /**
     * 更新配置并且缓存
     * 1、返回对象应为ElectricConfigDTO类型，可以将数据缓存到Redis中
     * @param electricConfigDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @CachePut(value = "getElectricConfigByCode", key = "T(com.ruihu.admin.constant.RedisConstants).queryElectricConfigByCodeKey(#electricConfigDTO.code)")
    public ResponseDTO<ElectricConfigDTO> updateAndCacheIt(ElectricConfigDTO electricConfigDTO) {
        ElectricConfigEntity entity = BeanCopierUtil.copy(electricConfigDTO, ElectricConfigEntity.class);
        entity.setUpdater(RequestTokenUtil.getRequestUser().getRequestUserId());
        entity.setUpdateTime(new Date());
        this.electricConfigDao.updateByIdIgnoreEmptyValue(entity);
        return ResponseDTO.succData(electricConfigDTO);
    }
}
